import { Commit, Getter, GetterTree, Action, ActionTree, MutationTree } from 'vuex'
import axios from 'axios';
import {SurgicalApplyGet,InfoGet,LevelCheckGet } from '@/store/common/patient'
import API from '@/api'



export interface State {
    surgicalApplyGetList: SurgicalApplyGet[],
    surgicalApplyGetAllList:SurgicalApplyGet[],
    InfoGetList: InfoGet[],
    InfoGetAllList:InfoGet[],
    LevelCheckGetList:LevelCheckGet[],
    LevelCheckGetAllList:LevelCheckGet[]
}

const initState: State = {
    surgicalApplyGetList: [],
    surgicalApplyGetAllList:[],
    InfoGetList:[],
    InfoGetAllList:[],
    LevelCheckGetList:[],
    LevelCheckGetAllList:[]
}

const surgicalApplyGet: Getter<State, any> = (state: State) => {
    return state.surgicalApplyGetList
}

const surgicalApplyGetAll: Getter<State, any> = (state: State) => {
    return state.surgicalApplyGetAllList
}

const infoGet: Getter<State, any> = (state: State) => {
    return state.InfoGetList
}

const infoGetAll: Getter<State, any> = (state: State) => {
    return state.InfoGetAllList
}

const levelCheckGet: Getter<State, any> = (state: State) => {
    return state.LevelCheckGetList
}

const levelCheckGetAll: Getter<State, any> = (state: State) => {
    return state.LevelCheckGetAllList
}

const surgicalApplyGetAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            params:{
                id:"13"
            },
            url: API.PatientSurgicalApply.Get
        }).then((response) => {
            // console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            context.commit('surgicalApplyGetM', response.data.result);
            resolve(response.data.result)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}

const surgicalApplyGetAllAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            url: API.PatientSurgicalApply.GetAll
        }).then((response) => {
            console.log("shuchu")
            console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            response.data.result.items.map((m: any) => {
                m.operations = JSON.parse(m.operations)
                let names : string[] = [];
                m.operations.forEach((n: any) => {
                    names.push(n.name)
                });
                m.operationNames = names;
            })
            context.commit('surgicalApplyGetAllM', response.data.result.items);
            resolve(response.data.result.items)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}

const infoGetAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            params:{
                id:"13"
            },
            url: API.PatientInfo.Get
        }).then((response) => {
            // console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            context.commit('infoGetM', response.data.result);
            resolve(response.data.result)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}

const infoGetAllAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            params:{
                id:"13"
            },
            url: API.PatientInfo.Get
        }).then((response) => {
            // console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            context.commit('infoGetAllM', response.data.result);
            resolve(response.data.result)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}

const levelCheckGetAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            params:{
                id:"13"
            },
            url: API.PatientLevelCheckItem.Get
        }).then((response) => {
            // console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            context.commit('LevelCheckGetM', response.data.result);
            resolve(response.data.result)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}

const levelCheckGetAllAction: Action<State, any> = 
(context: { commit: Commit }) => {
    return new Promise((resolve, reject) => {
        axios({
            url: API.PatientLevelCheckItem.GetAll
        }).then((response) => {
            console.log(response.data.result)
            // const payload: PatientInfo = response && response.data;
            response.data.result.items.map((m: any) => {
                m.operations = JSON.parse(m.operations)
                let names : string[] = [];
                m.operations.forEach((n: any) => {
                    names.push(n.name)
                });
                m.operationNames = names;
            })
            context.commit('LevelCheckGetAllM', response.data.result.items);
            resolve(response.data.result.items)
        }, (error) => {
            console.log(error);
            context.commit('profileError');
            reject(error)
        });
    });
}

const mutations: MutationTree<State> = {
    surgicalApplyGetM(state, list: SurgicalApplyGet[]) {
        state.surgicalApplyGetList = list
    },
    surgicalApplyGetAllM(state, list: SurgicalApplyGet[]) {
        state.surgicalApplyGetAllList = list
    },
    infoGetM(state, list: InfoGet[]) {
        state.InfoGetList = list
    },
    infoGetAllM(state, list: InfoGet[]) {
        state.InfoGetAllList = list
    },
    levelCheckGetM(state, list: LevelCheckGet[]) {
        state.LevelCheckGetList = list
    },
    levelCheckGetAllM(state, list: LevelCheckGet[]) {
        state.LevelCheckGetAllList = list
    },
};

const getters: GetterTree<State, any> = {
    surgicalApplyGet,
    surgicalApplyGetAll,
    infoGet,
    infoGetAll,
    levelCheckGet,
    levelCheckGetAll,
};

const actions: ActionTree<State, any> = {
    surgicalApplyGetAction, 
    surgicalApplyGetAllAction,
    infoGetAction,
    infoGetAllAction,
    levelCheckGetAction,
    levelCheckGetAllAction
}

export default {
    namespaced: true,
    state: initState,
    getters,
    actions,
    mutations,
}